<?php
/*
 * YekComputer (r) CMS
 * Copyright (c) 2009, YekComputer. All rights reserved.
 * Author : Babak Taraghi 'babak.taraghi@gmail.com'
 * Code licensed under the BSD License.
 * version: 1.0
 */

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class CategoriesModel extends CI_Model {
	
	var $tableName = "categories";
	var $langsTableName = "categories_langs";
	var $allChildren;
	
	
	/**
	 * @desc model constructor
	 */
	function CategoriesModel(){
		parent::__construct();
	}
	
	
	/**
	 * @desc will return a record from table
	 */
	function getRecord($id){
		$query = $this->db->get_where($this->tableName, array('id' => $id));
		return $query->row();
	}
	
	function getRecords($where){
		$this->db->order_by("order","asc");
		$query = $this->db->get_where($this->tableName, $where);
		return $query->result();
	}
	
	function searchLangs($keyword,$lang="en"){
		$this->db->where("language",$lang);
		$this->db->like('title', $keyword);
		$query = $this->db->get($this->langsTableName);
		return $query->result();
	}
	
	
	
	function getLangRecord($catId,$lang){
		$query = $this->db->get_where($this->langsTableName, array('cat_id' => $catId,"language"=>$lang));
		return $query->row();
	}
	
	
	function getLangRecordByInternalName($internalName,$lang){
		$query = $this->db->query("SELECT * FROM hajari_categories_langs WHERE `language` = '$lang' AND `cat_id` = (SELECT `id` FROM hajari_categories WHERE `internal_name` = '$internalName')");
		return $query->row();
	}
	
	/**
	 * 
	 */
	function getRecordByInternalName($internalName){
		$query = $this->db->get_where($this->tableName, array('internal_name' => $internalName));
		return $query->row();
	}
	
	function getChildren($id){
		$this->db->order_by("order","asc");
		$query = $this->db->get_where($this->tableName, array('parent_id' => $id));
		return $query->result();
	}
	
	function getAllChildren($parentRecord){
		if(!is_object($parentRecord))
			return false;
		$children = $this->getChildren($parentRecord->id);
		foreach($children as $child){
			$this->allChildren[$child->id] = $child;
			if($child->children > 0)
				$this->getAllChildren($child);
		}
		return $this->allChildren;
	}
	
	function insertLang($fieldsValues){
		return $this->db->insert($this->langsTableName, $fieldsValues);
	}
	
	function insert($parentId,$fieldsValues){
		$this->db->set('children', 'children+1', FALSE);
		$this->db->where('id', $parentId);
		$this->db->update($this->tableName);
		$fieldsValues["insert_time"] = time();
		return $this->db->insert($this->tableName, $fieldsValues);
	}
	
	function updateLang($catId,$lang,$fieldsValues){
		$this->db->where(array('cat_id'=>$catId,"language"=>$lang));
		return $this->db->update($this->langsTableName, $fieldsValues);
	}
	
	function update($id,$fieldsValues){
		$this->db->where('id', $id);
		return $this->db->update($this->tableName, $fieldsValues);
	}
	
	function deleteLang($catId){
		return $this->db->delete($this->langsTableName,array("cat_id"=>$catId));
	}
	
	function delete($id){
		$query = $this->db->get_where($this->tableName, array('id' => $id));
		$child = $query->row();
		$this->db->set('children', 'children-1', FALSE);
		$this->db->where('id', $child->parent_id);
		$this->db->update($this->tableName);
		return $this->db->delete($this->tableName,array("id"=>$id));
	}
	
}
?>